﻿#region Usings

using System;
using System.Text;
using System.Web.UI.WebControls;


#endregion

namespace Triad.Controls
{
    /// <summary>
    /// Combobox control
    /// </summary>
    /// <remarks>
    /// Author: tim.fischer,   8/25/2008 - 1:42 PM
    /// Modified By: 
    /// </remarks>
    public partial class tfComboBox 
    {
        private TextBox XMLtext;

        /// <summary>
        /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.
        /// </summary>
        /// <remarks>
        /// Author: tim.fischer,   8/25/2008 - 1:42 PM
        /// Modified By: 
        /// </remarks>
        protected override void CreateChildControls()
        {
            Controls.Clear();
            base.CreateChildControls();

            
            XMLtext = new TextBox {EnableViewState = true, ID = (ClientID+"XML"), Text = FilltfCombo()};
            XMLtext.Attributes["autocomplete"] = "off";
            XMLtext.TextChanged += raiseTextChanged;
            XMLtext.Style.Add("display", "none");

            ViewState[ClientID + "XML"] = XMLtext.Text;
            Controls.Add(XMLtext);

        }

        /// <summary>
        /// Loads the previously saved view state of the <see cref="T:System.Web.UI.WebControls.DetailsView"/> control.
        /// </summary>
        /// <param name="savedState">An <see cref="T:System.Object"/> that represents the state of the <see cref="T:System.Web.UI.WebControls.ListControl"/> -derived control.</param>
        /// <remarks>
        /// Author: tim.fischer,   8/25/2008 - 1:49 PM
        /// Modified By: 
        /// </remarks>
        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            //XMLtext.Text = ViewState[ClientID + "XML"].ToString();
            TextControl.Text = ViewState[ClientID + "XML"].ToString();
        }

        /// <summary>
        /// Gets the text control.
        /// </summary>
        /// <value>The text control.</value>
        /// <remarks>
        /// Author: tim.fischer,   8/25/2008 - 1:49 PM
        /// Modified By: 
        /// </remarks>
        protected internal TextBox TextControl
        {
            get
            {
                EnsureChildControls();
                return XMLtext;
            }
        }


        /// <exclude/>
        public override void Focus()
        {
            TextControl.Focus();
        }

        /// <summary>
        /// The container control of ComboBox's controls.
        /// </summary>
        /// <remarks>
        /// This is used by the designer.
        /// </remarks>
        protected internal WebControl ContainerControl
        {
            get
            {
                EnsureChildControls();
                return m_Container;
            }
        }



        private void raiseTextChanged(Object sender, EventArgs e)
        {
            //cache[ClientID+"txt"] = XMLtext.Text;
            OnTextChanged(e);
        }

        //protected override HtmlTextWriterTag TagKey
        //{
        //    get
        //    {
        //        return HtmlTextWriterTag.Div;
        //    }
        //}
        private string RegisterScript()
        {
            string strChkBox = string.Empty;
            string width = Width.Value.ToString().Replace("px", "");
            string height = Height.Value.ToString().Replace("px", "");
            if (height.Length < 2) height = "100";

            var jshtml = new StringBuilder();
            if (AllowMultiSelect) {strChkBox = ",'checkbox'";}
            else
                if (!String.IsNullOrEmpty(XtraDataField)) { strChkBox = ",'xdata'"; }
                else if (AllowImage) { strChkBox = ",'image'"; }
            jshtml.Append("var ");
            jshtml.Append(ClientID);
            jshtml.Append("=new tfComboBox('");
            jshtml.Append(ClientID);
            jshtml.Append("','");
            jshtml.Append(ClientID);
            jshtml.Append("','");
            jshtml.Append(width);
            jshtml.Append("','");
            jshtml.Append(DropDownWidth);
            jshtml.Append("','");
            jshtml.Append(height);
            jshtml.Append("','");
            jshtml.Append(_firstItemText);
            jshtml.Append("'");
            jshtml.Append(strChkBox);
            jshtml.Append(");");
            //jshtml.Append(ClientID);
            //jshtml.Append(".loadXMLString('<?xml version=\"1.0\" ?><list>");
            //jshtml.Append(this.XML);
            //jshtml.Append("</list>');");

            //jshtml.Append("CallbackRSD(");
            //jshtml.Append(ClientID);
            //jshtml.Append(".xmlList.value,");
            //jshtml.Append(ClientID);
            //jshtml.Append(");");

             jshtml.Append(ClientID);
             switch (ComboBoxType.ToString())
             {
                 case "ReadOnly":
                     jshtml.Append(".readonly(true);");
                     break;
                 case "AutoComplete":
                     jshtml.Append(".enableFilteringMode(true);");
                     break;
                 case "AutoFilter":
                     jshtml.Append(".enableFilteringMode(true);");
                     break;
                 case "TypeAhead":
                     jshtml.Append(".enableTypeAheadMode(true);");
                     break;
                 default:
                     break;
             }


            return jshtml.ToString();
        }
    }
}
